﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WeatherApp
{
    class NewWeatherData
    {
        private event Action<float, float, float> observers;
        private float temp, humidity, pressure;//温度 湿度 压力
        public WeatherData()
        {
            observers = new HashSet<IObserver>();
        }
        public void RegisterObserver(IObserver observer)
        {
            observers += observer.Update;
            //observers.Add(observer);
        }

        public void RemoveObserver(IObserver observer)
        {
            //observers.Remove(observer);
            observers -= observer.Update;
        }
        public void NotifyObserver()
        {
            observers?.Invoke(temp, humidity, pressure);//？.事件不为空 就执行
            //foreach (var item in observers)
            //{
            //    item.Update(temp, humidity, pressure);
            //}
        }
        public void MeasurementsChanged()
        {
            NotifyObserver();
        }

        public void SetMeasurements(float temp, float humidity, float pressure)
        {
            //设置数值 发生变化 通知所有观察者
            this.temp = temp;
            this.humidity = humidity;
            this.pressure = pressure;
            MeasurementsChanged();
        }
    }
}
